import abc
from abc import *

class BookInfo(object):
    url = ""
    navItemContent = ""
    content = ""

    def __init__(self, url, navItem, content):
        self.url = url
        self.navItemContent = navItem
        self.content = content




class ExportBook(metaclass=abc.ABCMeta):
    @abstractmethod
    def exportBookApi(self,book):
        pass



class ExportBookPdf(ExportBook):
    def __init__(self, book):
        pass
    def exportBookApi(self,book):
        print("export pdf file by ExportBookApi ")
        print(book.fileExtension)
        print(book.bookInfo.url)


class ExportBookMobi(ExportBook):
    def __init__(self, book):
        pass
    def exportBookApi(self,book):
        print("export mobi file by ExportBookApi")
        print(book.fileExtension)

class ExportBookHtml(ExportBook):
    def __init__(self, book):
        pass
    def exportBookApi(self,book):
        print("export html file by ExportBookApi")
        print(book.fileExtension)


class ExportBookOperate(ExportBook):
    @staticmethod
    def exportBookFactory(book):
        if isinstance(book, MobiBook):
            return ExportBookMobi(book)
        elif isinstance(book,PdfBook):
            return ExportBookPdf(book)
        elif isinstance(book,HtmlBook):
            return ExportBookHtml(book)
        else:
            print("其他格式暂不支持")


class Book(metaclass=abc.ABCMeta):
    bookInfo = ""
    fileExtension = ""
    name = ""


    def __init__(self, bookInfo, fmt):
        self.bookInfo = bookInfo
        self.fileExtension = "." + fmt
        self.name = self.calcBookName(fmt)
        self.prepare()

    @abstractmethod
    def createBook(self):
        '''
        createBook
        :制作电子书:
        '''

    def calcBookName(self, fmt):
        '''
        从参数里面计算出书的名字
        :return:
        '''
        print(self.bookInfo.url)
        filenameTempString = self.bookInfo.url.split("//")[1]
        filenameTempString = filenameTempString.replace(".", "_")
        filenameTempString = filenameTempString.replace("-", "_")
        filenameTempString = filenameTempString.replace("/", "_")
        file_name = filenameTempString + self.fileExtension

        return file_name

    def prepare(self):
        """
        准备工作
        :return:
        """
        # print(__doc__)
        print(self.name)


class PdfBook(Book):
    """
    pdf book
    """
    def createBook(self):
        """
        make pdf book func
        :return:
        """
        print(PdfBook.__doc__)


class MobiBook(Book):
    """
    Mobi book
    """
    def createBook(self):
        """
        make mobi book func
        :return:
        """
        print(MobiBook.__doc__)


class HtmlBook(Book):
    """html book """

    def createBook(self):
        """
        make html book func
        :return:
        """
        print(HtmlBook.__doc__)


class SimpleFactory(object):
    """
    简单工厂
    """

    @staticmethod
    def createBook(bookInfo, fmt):
        """
        简单工厂
        :param bookInfo:
        :param fmt:
        :return:
        """

        if 'pdf' in fmt:
            print("create pdf factory")
            return PdfBook(bookInfo, fmt)
        elif "mobi" in fmt:
            print("create mobi file")
            return MobiBook(bookInfo, fmt)
        elif 'html' in fmt:
            print("create html file")
            return HtmlBook(bookInfo, fmt)
        else:
            print("暂时支持pdf 和mobi和html")



class UrlParser:
    @abstractmethod
    def urlParserProcess(self):
        pass

class HtmlWithDocsParser(UrlParser):
    def urlParserProcess(self):
        print("urlParserProcess    HtmlWithDocsParser ")

class HtmlNoDocsParser(UrlParser):
    def urlParserProcess(self):
        print("urlParserProcess    HtmlNoDocsParser ")


class BookParserFactory:
    @abstractmethod
    def  makeBook(self):
        pass
    @abstractmethod
    def  urlParserFactory(self):
        pass

class DocsParserFactory(BookParserFactory):
    url = ""
    def __init__(self,url):
        self.url = url

    def makeBook(self):

        return MobiBook(BookInfo(self.url,"navitem","content"),"mobi")

    def urlParserFactory(self):
        return HtmlWithDocsParser()


class NoDocsParserFactory(BookParserFactory):
    url = ""

    def __init__(self, url):
        self.url = url

    def makeBook(self):
        return PdfBook(BookInfo(self.url,"navitem","content"),"pdf")

    def urlParserFactory(self):
        return HtmlNoDocsParser()


print("test now")
bookInfo = BookInfo("https://flutter.cn/docs", "nav_item", "content")
bookCreate = SimpleFactory.createBook(bookInfo, 'pdf')

bookExport = ExportBookOperate.exportBookFactory(bookCreate)
bookExport.exportBookApi(bookCreate)
print("-----------------------------")

noDocsParserFactory = NoDocsParserFactory("https://flutter.cn/")
noDocbook = noDocsParserFactory.makeBook().createBook()
print(noDocbook)
noDocsParserFactory.urlParserFactory().urlParserProcess()

docsParserFactory = DocsParserFactory("https://flutter.cn/docs")
docBook = docsParserFactory.makeBook().createBook()
print(docBook)
docsParserFactory.urlParserFactory().urlParserProcess()

